package com.ruoyi.quartz.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.dyun.tool.mybatis.domain.BaseEntity;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.quartz.CronUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import net.dreamlu.mica.core.validation.CreateGroup;
import net.dreamlu.mica.core.validation.UpdateGroup;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 * 定时任务调度表 sys_job
 *
 * @author dy
 */
@TableName("sys_job")
@Data
@EqualsAndHashCode(callSuper = true)
public class SysJob extends BaseEntity {

    private static final long serialVersionUID = 1L;

    /**
     * 任务名称
     */
    @NotBlank(message = "任务名称不能为空", groups = {CreateGroup.class, UpdateGroup.class})
    private String jobName;

    /**
     * 任务组名
     */
    private String jobGroup;

    /**
     * 调用目标字符串
     */
    @NotBlank(message = "调用目标字符串不能为空", groups = {CreateGroup.class, UpdateGroup.class})
    private String invokeTarget;

    /**
     * cron执行表达式
     */
    @NotBlank(message = "cron执行表达式不能为空", groups = {CreateGroup.class, UpdateGroup.class})
    private String cronExpression;

    /**
     * 计划执行错误策略（1立即执行 2执行一次 3放弃执行）
     */
    private String misfirePolicy;

    /**
     * 是否并发执行（0允许 1禁止）
     */
    private Integer concurrent;

    /**
     * 备注
     */
    private String remark;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    public Date getNextValidTime() {
        if (!StringUtils.isEmpty(cronExpression)) {
            return CronUtils.getNextExecution(cronExpression);
        }
        return null;
    }

}